<?php
/**
 *    This file is part of OXID eShop Community Edition.
 *
 *    OXID eShop Community Edition is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    OXID eShop Community Edition is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with OXID eShop Community Edition.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @link      http://www.oxid-esales.com
 * @package   admin
 * @copyright (C) OXID eSales AG 2003-2010
 * @version OXID eShop CE
 * @version   SVN: $Id: article_attribute.php 25640 2010-02-05 06:42:24Z alfonsas $
 */

/**
 * Admin article attributes/selections lists manager.
 * Collects available attributes/selections lists for chosen article, may add
 * or remove any of them to article, etc.
 * Admin Menu: Manage Products -> Articles -> Selection.
 * @package admin
 */
class Article_PersParam extends oxAdminDetails
{

    protected $ParamFields = array("details","SerialNo","IMEI");
    protected $ParamMode;
    
    /**
     * Collects article attributes and selection lists, passes them to Smarty engine,
     * returns name of template file "article_attribute.tpl".
     *
     * @return string
     */
    public function render()
    {
        parent::render();

        $oSmarty = oxUtilsView::getInstance()->getSmarty();
        $oSmarty->assign( "oViewConf", $this->_aViewData["oViewConf"]);
        $oSmarty->assign( "shop", $this->_aViewData["shop"]);
        
        $sOXID = oxConfig::getParameter("oxid");
        $sSql = "SELECT oxtitle, oxselvariant, oxpersparam FROM oxorderarticles WHERE oxid = '$sOXID'";
        //echo $sSql;
        $aProduct = array();
        $rs = oxDb::getDb(true)->Execute($sSql);
        array_push($aProduct, $rs->fields);
        
        $oSmarty->assign("oxid",$sOXID);
        $oSmarty->assign("oxtitle",$aProduct[0]['oxtitle']);
        $oSmarty->assign("oxselvariant",$aProduct[0]['oxselvariant']);
        $oSmarty->assign("aPersParam",  unserialize($aProduct[0]['oxpersparam']) );
        $oSmarty->assign("aParam", $this->ParamFields);
        $oSmarty->assign("sMode", $this->ParamMode);

        return "popups/article_persparam.tpl";
    }

    /**
     * Save all inventory data
     * 
     * @param type $sOXID
     * @param type $aParams 
     */
    public function saveparams($sOXID = null, $aParams = null)
    {
        if ( !isset( $sOXID ) && !isset( $aParams ) ) {
            $sOXID   = oxConfig::getParameter( "oxid" );
        }
        foreach ($this->ParamFields as $Param) {
            if (oxConfig::getParameter( "Pers_$Param" ) != '')
                $aParams[$Param] = oxConfig::getParameter( "Pers_$Param" );
        }
        
        $sParams = serialize($aParams);

        $oDb = oxDb::getDb();
        $sSql = "UPDATE oxorderarticles "
                . "SET oxpersparam = '$sParams' "
                . "WHERE "
                . "oxid = '$sOXID' ";
        //echo $sSql.'<br>';
        $oDb->execute($sSql);
        
        $this->ParamMode = "saved";
        
        return;
        
    }
}
